#!/bin/bash

export MaoPth="/home/$LOGNAME/maotoolbox/"

my_chmod(){
	chmod -R 777 $1
	chmod +x $1
}

my_replace_svg(){
	ks_sfn=('colorized' 'material-dark' 'material-darker' 'material-light' 'z-bolt')
	for var in ${ks_sfn[@]}
	do
		if [ -e "/home/$LOGNAME/KlipperScreen/styles/$var/images/klipper.svg" ]; then
			sudo rm -rf "/home/$LOGNAME/KlipperScreen/styles/$var/images/klipper.svg"
			sudo cp $MaoPth"klipper.svg" "/home/$LOGNAME/KlipperScreen/styles/$var/images/klipper.svg"
		fi
	done
}

ban_temperaturehost(){
	if [ -d "/home/umeko" ]; then
		echo "36000" > /home/umeko/temp
		sed -i 's/\/sys\/class\/thermal\/thermal_zone0\/temp/\/home\/umeko\/temp/g' /home/umeko/klipper/klippy/extras/temperature_host.py
	fi
}
prohibit_updates(){
	sudo sed -i '/\[update_manager\]/,$d' "/home/$LOGNAME/printer_data/config/moonraker.conf"
}

install_service(){
	my_chmod $MaoPth'start.sh'
	my_chmod $MaoPth'census.sh'
	my_chmod $MaoPth'usbdriveini.sh'
	my_chmod $MaoPth'backupconfig.py'
	
	if [ -e "/etc/systemd/system/censusservice.service" ]; then
		sudo systemctl stop censusservice.service
		sudo rm -rf '/etc/systemd/system/censusservice.service'
	fi
	if [ -e "/etc/systemd/system/upanservice.service" ]; then
		sudo systemctl stop upanservice.service
		sudo rm -rf '/etc/systemd/system/upanservice.service'
	fi
	if [ -e "/etc/systemd/system/maotoolbox.service" ]; then
		sudo systemctl stop maotoolbox.service
		sudo rm -rf '/etc/systemd/system/maotoolbox.service'
	fi
	
	sed -i "s/umeko/$LOGNAME/g" $MaoPth"start.sh"
	sed -i "s/umeko/$LOGNAME/g" $MaoPth"usbdriveini.sh"
	sed -i "s/umeko/$LOGNAME/g" $MaoPth"maotoolbox.service"
	sed -i "s/umeko/$LOGNAME/g" $MaoPth"backupconfig.py"
	
	cp $MaoPth'maotoolbox.service' '/etc/systemd/system/maotoolbox.service'
	
	sudo systemctl daemon-reload
	sudo systemctl start maotoolbox.service
	sudo systemctl enable maotoolbox.service
}

if [ -e "/etc/systemd/system/censusservice.service" ]; then
	echo "usbdrive ok"
else
	mkdir "/home/$LOGNAME/printer_data/gcodes/usbdrive"
fi

ban_temperaturehost
prohibit_updates
my_replace_svg
install_service

reboot